/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package company.search.beans;

import beans.LoginBean;
import company.abstractClasses.CompanyAbstractEntityList;
import company.folder.beans.NewFolderListBean;
import entities.Candidate;
import facades.CandidateFacade;
import facades.SearchFacade;
import entities.CompanyVisitedProfile;
import entities.Search;
import facades.CompanyVisitedProfileFacade;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.transaction.Transactional;
import qualifiers.CreatedEntityEvent;
import qualifiers.EditedEntityEvent;
import qualifiers.RemovedEntityEvent;
import qualifiers.ReportedProfile;
import sqlgenerators.search.SearchSQLGenerator;
import sqlgenerators.search.SearchSqlGeneratorInterface;

/**
 *
 * @author damfercer
 */
@Named
@SessionScoped
public class NewSearchListBean extends CompanyAbstractEntityList<Search, SearchFacade> implements Serializable {
    @Inject
    private CandidateFacade candidateEJB;
    private Candidate selectedCandidate;      
    @Inject
    @ReportedProfile
    private Event<Candidate> candidateReported;
    @Inject
    CompanyVisitedProfileFacade companyVisitedEJB;
    @Inject
    @CreatedEntityEvent
    private Event<CompanyVisitedProfile> companyVisitedProfileEvent;
    @Inject
    @RemovedEntityEvent
    private Event<Search> removedSearchEvent;
    private SearchSqlGeneratorInterface sqlGenerator;
    @Inject
    @EditedEntityEvent
    private Event<Search> editedSearchEvent;
    @Inject
    LoginBean loginBean;
    @Inject
    NewFolderListBean newFolderListBean;
    
    @PostConstruct
    public void initNewSearchListBean(){
        sqlGenerator=new SearchSQLGenerator();
    }
    
    @Override
    public void initEntityList(){
        setEntityList(getCompany().getSearchList());
    }
    
    public void getSearchFields(Search search){
        String sql=sqlGenerator.getSqlFromSearch(search);
        if(search.getPreviewCandidateList()==null){
            search.setPreviewCandidateList(candidateEJB.getPreviewSearchCandidates(sql, 6, search.getCompany()));
        }
        if(search.getValidCandidates()==null){
            search.setValidCandidates(candidateEJB.getNumberOfValidCandidates(sql, search.getCompany()));
        }
        if(search.getCandidatesSinceLastView()==null){
            search.setCandidatesSinceLastView(candidateEJB.getNumberOfValidCandidatesFromLastSearchView(sql, search.getCompany(),search));
        }
    }
    
    public void deleteSearch(){
        getCompany().getSearchList().remove(getChosenEntity());
        getEntityFacade().remove(getChosenEntity());
        removedSearchEvent.fire(getChosenEntity());
    }
    
    @Transactional
    public void setSelectedSearchAndCandidate(Search search,Candidate candidate){
        candidate.setVisits(candidate.getVisits()+1);
        candidateEJB.edit(candidate);
        CompanyVisitedProfile visitedProfile=new CompanyVisitedProfile();
        visitedProfile.setIdCandidate(candidate);
        visitedProfile.setIdCompany(getCompany());
        visitedProfile.setVisitDate(new Date());
        companyVisitedEJB.create(visitedProfile);
        if(candidate.getCompanyVisitedProfileList()==null){
            candidate.setCompanyVisitedProfileList(new ArrayList<CompanyVisitedProfile>());
        }
        candidate.getCompanyVisitedProfileList().add(visitedProfile);
        if(getCompany().getCompanyVisitedProfileList()==null){
            getCompany().setCompanyVisitedProfileList(new ArrayList<CompanyVisitedProfile>());
        }
        getCompany().getCompanyVisitedProfileList().add(visitedProfile);
        companyVisitedProfileEvent.fire(visitedProfile);        
        selectedCandidate=candidate;
    }
    
    public Integer dateDifference(Date compare){
        Date now=new Date();
        long diff=now.getTime()-compare.getTime();
        long days=diff / 1000 / 60 / 60 / 24 / 365;
        return (int)days;
    }
    
    public void prepareNewSearch(){
        chooseEntity(new Search());
    }
    
    public void viewSearch(Search s){
        setChosenEntity(s);
        s.setLastSearchDate(new Date());
        editSearch();
        chooseEntity(getChosenEntity());
    }

    public void editSearch(){
        getEntityFacade().edit(getChosenEntity());
        editedSearchEvent.fire(getChosenEntity());
    }
    
    /**
     * @return the selectedCandidate
     */
    public Candidate getSelectedCandidate() {
        return selectedCandidate;
    }

    /**
     * @param selectedCandidate the selectedCandidate to set
     */
    public void setSelectedCandidate(Candidate selectedCandidate) {
        this.selectedCandidate = selectedCandidate;
    }
    
    public void reportSelectedCandidate(){
        candidateReported.fire(selectedCandidate);
    }        

    @Override
    public String redirectToViewEntity() {
        return "/views/company/search/NewCreateSearchView?faces-redirect=true";
    }

}
